package pokercard;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {

    public String suit;
    public int rank;

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public Card() {};

    @Override
    public String toString() {
        return  suit + '\'' +
                rank + " ";
    }
}

public class Poker {

    public static String[] suits = new String[] {"♠", "♥", "♣", "♦"};

    public List<List<Card>> allocateCard (List<Card> cards) {
        List<Card> man1 = new ArrayList<>();
        List<Card> man2 = new ArrayList<>();
        List<Card> man3 = new ArrayList<>();

        List<List<Card>> retAllocation = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            man1.add(cards.get(++i));
            cards.remove(i);
            man2.add(cards.get(++i));
            cards.remove(i);
            man3.add(cards.get(i));
            cards.remove(i);
        }
        retAllocation.add(man1);
        retAllocation.add(man2);
        retAllocation.add(man3);
        return retAllocation;
    }

    public List<Card> initiateCards (){
        //Card[] cards = new Card[52];

        /**
         * 为什么不写ArrayList<Card> cards = new ArrayList<>(52);
         */
        List<Card> cards = new ArrayList<>(52);
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 4; j++) {
                Card card = new Card(suits[j], i+1);
                cards.add(card);
            }
        }

        /**
         * cards.size() == 0 所以循环无法执行，不知道原因是什么
         * 只能通过.add()来增加.size()
         */
//        for(int i = 0; i < cards.size(); i++) {
//            for(int j = 0; j <= Poker.suits.length; j++) {
//                Card card = new Card(suits[j], j+1);
//                cards.add(card);
//
////                cards.get(i).rank = j + 1;
////                cards.get(i).suit= Poker.suits[j];
////                i++;
//            }
//        }
        return cards;
    }

    public void shuffle(List<Card> cards) {
        Random random = new Random();
        int r = 0;
        Card tmp = new Card();
        for(int i = 0; i < cards.size(); i++) {
            r = random.nextInt(cards.size());
            tmp = cards.get(r);
            cards.set(r, cards.get(i));
            cards.set(i, tmp);
        }
    }

}
